import java.util.*;
import java.util.stream.Collectors;

/**
 * @author lfg
 * @version 1.0
 */
public class Main {

    public static void main(String[] args) {

        //任务i的难度
        int[] level = {1, 2, 3, 4, 7};
        //任务i的佣金
        int[] commission = {2, 1, 15, 20, 25};
        //小弟j的工作能力
        int[] worker = {2, 3, 4, 6};

        System.out.println(maxCommission(level, commission, worker));

    }


    public static Integer maxCommission(int[] level, int[] commission, int[] worker) {
        //参数校验：如果状态不对，或者佣金不对，可以返回0
        if (!checkParams(level, commission, worker)) {
            return 0;
        }
        // 佣金倒序排序 Map<佣金下标, 佣金>
        Map<Integer, Integer> sortCommissionMap = sortCommissionMap(commission);
        // 计算最大佣金
        return Arrays.stream(worker)
                .boxed()
                .sorted(Collections.reverseOrder())
                .mapToInt(workerValue -> {
                    //下标（佣金由高到低排序）
                    List<Integer> sortIndex = new ArrayList<>(sortCommissionMap.keySet());
                    for (Integer index : sortIndex) {
                        //小弟的能力worker[j]只能完成小于或者等于worker[j]的任务
                        if (workerValue.compareTo(level[index]) >= 0) {
                            return sortCommissionMap.get(index);
                        }
                    }
                    return 0;
                }).sum();
    }

    /**
     * 参数格式校验
     *
     * @param level      难度
     * @param commission 佣金
     * @param worker     小弟工作力
     * @return true / false
     */
    private static boolean checkParams(int[] level, int[] commission, int[] worker) {


        //null校验
        if (level == null || commission == null || worker == null) {
            return false;
        }

        //难度包含负数
        boolean levelContainFalse = Arrays.stream(level).boxed().map(levelValue -> levelValue.compareTo(0) >= 0).anyMatch(bool -> !bool);
        if (levelContainFalse) {
            return false;
        }

        //佣金包含负数
        boolean commissionContainFalse = Arrays.stream(commission).boxed().map(commissionValue -> commissionValue.compareTo(0) >= 0).anyMatch(bool -> !bool);
        if (commissionContainFalse) {
            return false;
        }

        //工作力包含负数
        boolean workerContainFalse = Arrays.stream(worker).boxed().map(workerValue -> workerValue.compareTo(0) >= 0).anyMatch(bool -> !bool);
        if (workerContainFalse) {
            return false;
        }

        //非空校验
        if (commission.length == 0 || worker.length == 0 || level.length == 0) {
            return false;
        }

        //长度校验
        if (level.length != commission.length) {
            return false;
        }

        return true;
    }

    /**
     * 佣金倒序排序
     *
     * @param commission 佣金数组
     * @return Map<佣金下标, 佣金>
     */
    private static Map<Integer, Integer> sortCommissionMap(int[] commission) {
        Map<Integer, Integer> commissionMap = new HashMap<>(commission.length, 1f);
        for (int i = 0; i < commission.length; i++) {
            commissionMap.put(i, commission[i]);
        }
        return commissionMap.entrySet()
                .stream()
                .sorted(Map.Entry.comparingByValue(Collections.reverseOrder()))
                .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (o, n) -> n, LinkedHashMap::new));
    }

}
